/**********************************************************************
 * SailmiPIM - modular cross-platform feature Personal Information Manager Tools.
 * Copyright(C) 2006-2014  Georg Rudoy
 *
 * Distributed under the Boost Software License, Version 1.0.
 *(See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
 **********************************************************************/

#include "logindialog.h"
#include <QDomDocument>
#include "util/sys/sysinfo.h"
#include "sailmipim/ihavediaginfo.h"
#include "sailmipim/core/iusermanager.h"
#include "sailmipim/core/iuserproxy.h"
#include "sailmipim/core/icoreproxy.h"
#include <QMessageBox>
#include "registerdialog.h"
namespace SailmiPIM::Plugin::UserProxy
{
    LoginDialog::LoginDialog(QWidget *parent)
    : QDialog(parent)
	{
        userProxy = new UserProxyImp(this);
        Ui_.setupUi(this);
	}


    void LoginDialog::accept()
    {
        //点击登录，执行登录操作，并更改UserManager中UserInfo信息
        //关闭窗口
        Ui_.label_hint->setText("");
        if(Ui_.lineEdit_account->text().isEmpty()){
            Ui_.label_hint->setText("account is empty!");
            return;
        }
        if(Ui_.lineEdit_password->text().isEmpty()){
            Ui_.label_hint->setText("password is empty!");
            return;
        }
        UserInfo *userInfo = userProxy->login(Ui_.lineEdit_account->text(),Ui_.lineEdit_password->text());
        if(userInfo!=NULL&&userInfo->isLogined){
            qDebug() << "LoginDialog::on_buttonBox_accepted->" << userInfo->userName;
            GetProxyHolder()->GetUserManager()->setUserInfo(userInfo);
            QDialog::accept();
        }else{
            Ui_.label_hint->setText("login failed!");
            return;
        }
    }


    void LoginDialog::reject()
    {
        this->close();
        QApplication::exit();
    }



    void LoginDialog::on_registered()
    {
        this->show();
    }

    void LoginDialog::on_linkbutton_register_clicked()
    {
        RegisterDialog *registerDialog = new RegisterDialog();
        connect(registerDialog,&RegisterDialog::registerSuccess,this,&LoginDialog::on_registered);
        this->hide();
        registerDialog->show();

    }


}
